package connectDB;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import entities.Accountentities;


public class AccountDB extends AbstractDB  implements IDB<Accountentities,Boolean,String>{

	@Override
	public Boolean add(Accountentities e) {
		String sql="insert USER values(?,?)";
		Connection con = this.condb.getConnectDB();
		PreparedStatement pst;
		try {
			pst = con.prepareStatement(sql);
			//pst.setString(1, e.getUsername());
		//	pst.setString(2, e.getPassword());
			return pst.executeUpdate() >0 ;
			
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		return false;
	}

	@Override
	public Boolean delete(String k) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Boolean update(Accountentities e) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Accountentities find(String k) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Accountentities> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	public Boolean checkAccount(Accountentities e)
	{	String sql ="select * from [USER] where UserId=? and password=?";
		Connection con = this.condb.getConnectDB();
		PreparedStatement pst ;
		
		try {
			pst = con.prepareStatement(sql);
			pst.setString(1, e.getUserid());
			//pst.setString(2,crypt(e.getPassword()));
			pst.setString(2,e.getPassword());
			ResultSet rs  = pst.executeQuery();
			if(rs.next())
			{
				return true;
				
			}
			rs.close();
			pst.close();
			con.close();
	
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		return false;
	}
	
	
	//phan quyen
	public Boolean checkphanquyen(Accountentities e)
	{	String sql ="select * from [USER] where UserId=? and password=? and permission=?";
		Connection con = this.condb.getConnectDB();
		PreparedStatement pst ;
		
		try {
			pst = con.prepareStatement(sql);
			pst.setString(1, e.getUserid());
			//pst.setString(2,crypt(e.getPassword()));
			pst.setString(2,e.getPassword());
			pst.setString(3, e.getPermission());
			ResultSet rs  = pst.executeQuery();
			if(rs.next())
			{
				return true;
				
			}
			rs.close();
			pst.close();
			con.close();
	
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		return false;
	}

	@Override
	public Boolean delete1(int i) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Accountentities find1(int k) {
		// TODO Auto-generated method stub
		return null;
	}
	
}
